// pages/user/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:false,//筛选弹框
    pxList:['综合排序','销量最高','距离最近','好评优选','起送价最低','配送费最低','人均从低到高','人均从高到低','通用排序'],//排序
    px:0,//综合排序默认值(按数组下标)
    flList:[//分类列表
      {name:'全部分类',children:[]},
      {name:'快餐便当11111',children:[{name:'全部'},{name:'盖浇饭'},]},
      {name:'中式菜系',children:[{name:'全部'},{name:'川湘菜'},{name:'粤菜'},{name:'东北菜'},{name:'云南菜'},{name:'云南菜1'},{name:'云南菜2222'},{name:'云南菜3'}]}
    ],
    fl:0,//分类下表
    flVal:'全部分类',//分类值
    sudu:false,//速度
    addressMsg:{},//定位地址信息
    time:{
      leftd:'00',
      lefth:'00',
      leftm:'00',
      lefts:'00'
    },//限时秒杀
    timer:null,
    statusBarHeight: app.globalData.height + 'px',
    navigationBarHeight: (app.globalData.height + 44) + 'px',
    bgColor:'#FFD101',
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],//轮播图
    indicatorDots: true,//轮播小圆点显示
    autoplay: true,//轮播自动播放
    interval: 3000,//自动切换时间
    typeList:[
      {name:'美食外卖',img:'../../../static/images/mswm.png',type:1},
      {name:'超市便利',img:'../../../static/images/csbl.png',type:2},
      {name:'限时秒杀',img:'../../../static/images/xsms.png',type:3},
      {name:'快递跑腿',img:'../../../static/images/kdpt.png',type:4},
      {name:'休闲玩乐',img:'../../../static/images/xxwl.png',type:5},
      {name:'简餐便当',img:'../../../static/images/jcbd.png',type:6},
      {name:'水果生鲜',img:'../../../static/images/sgsx.png',type:7},
      {name:'炸鸡烧烤',img:'../../../static/images/zjsk.png',type:8},
      {name:'粉面粥点',img:'../../../static/images/fmzd.png',type:9},
      {name:'丰富宵夜',img:'../../../static/images/ffxy.png',type:10},
      {name:'汉堡披萨',img:'../../../static/images/hbps.png',type:11},
      {name:'香锅冒菜',img:'../../../static/images/xgmc.png',type:12},
      {name:'奶茶糕点',img:'../../../static/images/ncgd.png',type:13},
      {name:'馄饨水饺',img:'../../../static/images/hdsj.png',type:14},
      {name:'轻食西餐',img:'../../../static/images/qsxc.png',type:15}
    ],
    tabList: [
      {name:'附近推荐',id:1},
      {name:'发现好菜',id:2},
      {name:'超市',id:3},
      {name:'水果',id:4},
      {name:'奶茶',id:5},
      {name:'轻食',id:6}
    ],
    tabIndex:1,
    shopList: [],//店铺列表
    pageSize:5,//分页每页显示条数
    pageNum:1,//当前页数
    total:20,//总条数
    activeIdx:0
  },
  goBack(){
    //返回主入口
    wx.switchTab({
      url: '/pages/homeIndex/index',
    })
  },
  goXsms(){
    wx.navigateTo({
      url: '../../timeSeckill/index',
    })
  },
  goYhd(){
    wx.navigateTo({
      url: '../../goodShop/index',
    })
  },
  //2-23 修改主页入口地址为点击事件
  goRuKou(event){
    var item = event.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/user/msym/index?name='+item.name,
    })
  },
  //筛选--排序
  checkedTapPx (e) {
    var px = e.currentTarget.dataset.index
    this.setData({
      px,
      isShow:0
    })
  },
  changeFl(e){
    //分类
    var fl = e.currentTarget.dataset.index
    if(fl==0){
      this.setData({
        isShow:0,
        flVal:this.data.flList[fl].name
      })
    }
    this.setData({
      fl
    })
  },
  checkedTapFl(e){
    var flVal = e.currentTarget.dataset.item.name
    this.setData({
      flVal,
      isShow:0
    })
  },
  changeSudu(){
    this.setData({
      sudu:!this.data.sudu,
      isShow:3
    })
  },
  //关闭/打开 弹框
  changeShow(event){
    var dataset=event.currentTarget.dataset
    this.setData({
      isShow:dataset.val
    })
  },
  //限时秒杀
  
  setTime(){
    var nowtime = new Date(),  //获取当前时间
      endtime = new Date("2021/2/17 21:42");  //定义结束时间
    var lefttime = endtime.getTime() - nowtime.getTime(),  //距离结束时间的毫秒数
      leftd = Math.floor(lefttime/(1000*60*60*24)),  //计算天数
      lefth = Math.floor(lefttime/(1000*60*60)%24),  //计算小时数
      leftm = Math.floor(lefttime/(1000*60)%60),  //计算分钟数
      lefts = Math.floor(lefttime/1000%60);  //计算秒数
      leftd=leftd>9?leftd:'0'+leftd
      lefth=lefth>9?lefth:'0'+lefth
      leftm=leftm>9?leftm:'0'+leftm
      lefts=lefts>9?lefts:'0'+lefts
      if(lefttime<=0){
        clearInterval(this.data.timer)
        return false
      }
      this.setData({
        time:{
          leftd,
          lefth,
          leftm,
          lefts
        }
      })
    // return leftd + "天" + lefth + ":" + leftm + ":" + lefts;  //返回倒计时的字符串
  },
  changeTab(event){
    //切换tab
    var dataset=event.currentTarget.dataset
    this.setData({
      tabIndex:dataset.id
    })
  },
  onPageScroll: function (e) {
    //监听滑动
    if (e.scrollTop < 30) {
      this.setData({
        bgColor:'#FFD101'
      })
    } else if (e.scrollTop > 40) {
      this.setData({
        bgColor:'white'
      })
    }
  },
  //跳转搜索页面
  goSearch(){
    wx.navigateTo({
      url: '/pages/user/searchResult/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.watch(this, {
      addressMsg: function (newVal) {
        //监听定位地址变化--刷新当前页
        
        
        setTimeout(function () {
          wx.showLoading({
            title: '加载中',
            mask: true
          })
        }, 100)
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      }
    })
    //限时秒杀--定时器开始
    let timer=setInterval ( ()=> {
        this.setTime()
    }, 1000);  //反复执行函数本身
    this.setData({
      timer
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉了')
    setTimeout(()=>{
      wx.stopPullDownRefresh()
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.total>this.data.shopList.length){
      setTimeout(()=>{
        let arr=[
          {name:'烤肉',score:'4.7',num:'478',send:'15',sendMoney:'0.9',time:'42',juli:'1.8'},
          {name:'烤肉',score:'4.7',num:'478',send:'15',sendMoney:'0.9',time:'42',juli:'1.8'},
          {name:'烤肉',score:'4.7',num:'478',send:'15',sendMoney:'0.9',time:'42',juli:'1.8'},
          {name:'烤肉',score:'4.7',num:'478',send:'15',sendMoney:'0.9',time:'42',juli:'1.8'},
          {name:'烤肉',score:'4.7',num:'478',send:'15',sendMoney:'0.9',time:'42',juli:'1.8'}
        ]
        let data=this.data.shopList.concat(arr)
        this.setData({
          shopList:data
        })
      },1000)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})